Vue.prototype.$http=axios;
axios.defaults.baseURL="http://localhost:8080";

//1.axios前置拦截器：每次通过axios发送请求之前，让所有的请求都携带uToken
axios.interceptors.request.use(res=>{
    //从浏览器中获取token
    let token = localStorage.getItem("token");
    if(token){
        //将token添加到请求头
        res.headers["token"] = token;
    }
    return res;//语法格式，必须返回
})

//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    //后端响应的是没有登录的信息
    if (false === res.data.success && ("noLogin" === res.data.msg || "timeout" === res.data.msg)) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="/login.html";
    }
    return res;
}, error => {
    Promise.reject(error)
})
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//

let url = location.href;
let path = url.substring(url.lastIndexOf("/"));
if (path != "/login.html" && path != "/register.html" && url.indexOf("binder.html")==-1 && url.indexOf("callback.html")==-1){
    let logininfo = localStorage.getItem("logininfo");
    if (!logininfo){
        location.href='/login.html'
    }
}

function parseUrlParams2Obj(url){
    var obj = {};
    let params = url.substring(url.indexOf("?")+1);
    let paramsArr = params.split("&");
    for (var i = 0; i < paramsArr.length; i++){
        let param = paramsArr[i];
        let paramArr = param.split("=");
        obj[paramArr[0]] = paramArr[1]
    }
    return obj;
}

/*console.log(parseUrlParams2Obj("html?code=071Iyf1w3Ia9503nwH0w3bOGOq4Iyf1j&state=1"))*/
